from paddleocr import PaddleOCR
import cv2
import os
import logging


def sfz_ocr_predict(img):
    '''img is cv2 image object'''
    try:
        ocr = PaddleOCR(cls_thresh=0, use_gpu=True, 
                        det_model_dir='./inference/sfz_det_infer', 
                        rec_model_dir='./inference/ch_PP-OCRv2_rec_infer', 
                        cls_model_dir='./inference/ch_ppocr_mobile_v2.0_cls_infer')
        res_ls = []
        try:
            result = ocr.ocr(img)
            for line in result:
                res_ls.append(line[1][0])
        except Exception as e:
            logging.exception(e)
        return res_ls
    except Exception as e:
        logging.exception(e)
        return []


if __name__ == '__main__':
    img = cv2.imread(r'E:\\train-50-new\\8001-front.jpg')
    res = sfz_ocr_predict(img)
    print(res)